﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Kairos.Views
{
    public static class ButtonExtensions
    {
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, (string)null, new RouteValueDictionary(), (IDictionary<string, object>)new RouteValueDictionary());
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, (string)null, new RouteValueDictionary(routeValues), (IDictionary<string, object>)new RouteValueDictionary());
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, (string)null, new RouteValueDictionary(routeValues), (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, (string)null, routeValues, (IDictionary<string, object>)new RouteValueDictionary());
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, (string)null, routeValues, htmlAttributes);
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(), (IDictionary<string, object>)new RouteValueDictionary());
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(routeValues), (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return MvcHtmlString.Create(ButtonExtensions.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string)null, actionName, controllerName, routeValues, htmlAttributes));
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, object htmlAttributes)
        {
            return ButtonExtensions.ButtonLink(htmlHelper, linkText, actionName, controllerName, protocol, hostName, fragment, new RouteValueDictionary(routeValues), (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString ButtonLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return MvcHtmlString.Create(ButtonExtensions.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string)null, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes));
        }
        public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return ButtonExtensions.GenerateLink(requestContext, routeCollection, linkText, routeName, actionName, controllerName, (string)null, (string)null, (string)null, routeValues, htmlAttributes);
        }
        public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return ButtonExtensions.GenerateLinkInternal(requestContext, routeCollection, linkText, routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes, true);
        }
        private static string GenerateLinkInternal(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool includeImplicitMvcValues)
        {
            string str = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues);
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.Attributes.Add("type", "button");
            tagBuilder.Attributes.Add("value", !string.IsNullOrEmpty(linkText) ? HttpUtility.HtmlEncode(linkText) : string.Empty);
            tagBuilder.MergeAttributes<string, object>(htmlAttributes);
            tagBuilder.MergeAttribute("onclick", "window.location.href='" + str + "'");
            return tagBuilder.ToString();
        }
    }
}